SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE Procedure [dbo].[SP_TMSABA_MENS_U01]
/*-------------------------------------------------------*/
/*---Empresa              : OFISIS S.A.                --*/
/*---Cliente              : OFISIS                 --*/
/*---Sistema              : Tesoreria                  --*/
/*---M¸dulo               : Empresa                    --*/
/*---Programa             : Reprocesamiento del Mes    --*/
/*---Script               : tmsaba_m.sql               --*/
/*---Nombre SP            : SP_TMSABA_MENS_U01         --*/
/*---Desarrollado por     : Javier Flores A.           --*/
/*---Fecha Creaci¸n       : 16/07/1999                 --*/
/*---Base Datos           : Microsoft Sql Server       --*/
/*---Versi¸n              : 7.0                        --*/
/*---Invoca a SP          :                            --*/
/*-------------------------------------------------------*/
/*-------------------------------------------------------*/
/*---Modificado 1 por     : DCH                            --*/
/*---Fecha Modificaci¸n   : 9/11/2004                       --*/ 
/*---Detalle Modificaci¸n : Actualizacion de Nuevo Mundo   --*/
/*-------------------------------------------------------*/

/*-------------------------------------------------------*/
/*---Comentarios                                       --*/
/*---                                                  --*/
/*---                                                  --*/
/*-------------------------------------------------------*/

/*-------------------------------------------------------*/
/*---Drop Proc SP_TMSABA_MENS_U01                      --*/
/*---Grant Execute on SP_TMSABA_MENS_U01 to public     --*/ 
/*---SP_TMSABA_MENS_U01                                --*/
/*-------------------------------------------------------*/
 @ISCO_EMPR TD_VC_002, 
 @ISCO_USUA_MODI TD_VC_008_USUA,
 @ISCO_BANC TD_VC_003, 
 @ISNU_CNTA_BANC TD_VC_020,
 @ISAA_BNCO TD_VC_004, 
 @ISMM_BNCO TD_VC_002
	As
Declare  @CSCO_BANC TD_VC_003, @CSNU_CNTA_BANC TD_VC_020, 
          @CSAA_BNCO TD_VC_004, @CSMM_BNCO TD_VC_002, 
          @CNIM_SALD_INIC TD_NU_016_004, @CNIM_INGR TD_NU_016_004, @CNIM_EGRE TD_NU_016_004,
          @VNCO_0001 TD_IN_001, @VNCO_0002 TD_IN_001, 
          @VNIM_AU01 TD_NU_016_004, @VNIM_AU02 TD_NU_016_004, @CSTI_SITU TD_VC_003


Begin Tran SP_TMSABA_MENS_U01


    Declare	CU_TMSABA_MENS_U01 Cursor
    For	
    Select CO_BANC, NU_CNTA_BANC, AA_BNCO, MM_BNCO, IM_SALD_INIC, IM_INGR, IM_EGRE, TI_SITU
    From   TMSABA_MENS
    Where  CO_EMPR = @ISCO_EMPR 
    And    CO_BANC = @ISCO_BANC 
    And    NU_CNTA_BANC = @ISNU_CNTA_BANC 
    And    AA_BNCO+MM_BNCO >= @ISAA_BNCO+@ISMM_BNCO
                
    Open  CU_TMSABA_MENS_U01
    Fetch CU_TMSABA_MENS_U01 into @CSCO_BANC, @CSNU_CNTA_BANC,
    @CSAA_BNCO, @CSMM_BNCO, @CNIM_SALD_INIC, @CNIM_INGR, @CNIM_EGRE, @CSTI_SITU

    Select @VNCO_0001 = 0, @VNCO_0002 = 0, @VNIM_AU01 = 0, @VNIM_AU02 = 0

    While (@@FETCH_STATUS = 0)
      Begin
	  If (@@FETCH_STATUS < 0)
   	      Begin
	        RAISERROR 20001 'FIN DE ARCHIVO'
		  Close CU_TMSABA_MENS_U01
		  Deallocate CU_TMSABA_MENS_U01
		  Return
      	End
  
        If @VNCO_0001 >= 1       
           Begin
			Update TMSABA_MENS
			Set IM_SALD_INIC = @VNIM_AU01
			Where CO_EMPR = @ISCO_EMPR 
			AND   CO_BANC = @CSCO_BANC 
			AND   NU_CNTA_BANC = @CSNU_CNTA_BANC
			AND   AA_BNCO = @CSAA_BNCO
			AND   MM_BNCO = @CSMM_BNCO
			
			If @@error <> 0
			 Begin                   
			   Close CU_TMSABA_MENS_U01
			   Deallocate CU_TMSABA_MENS_U01
			   Rollback Tran
			   Return
			 End
	       End       
       
            /*  -------- LA PRIMERA VEZ : CALCULO EL SALDO INICIAL PARA EL SGTE MES --------- */      
   
            If @VNCO_0001 = 0
               Select @VNIM_AU01 =  (ISNULL(@CNIM_SALD_INIC,0) + ISNULL(@CNIM_INGR,0)) - ISNULL(@CNIM_EGRE,0)

            /* --------- CALCULO : DEL SALDO INICIAL PARA LOS SGTES MESES -------------------- */

            If @VNCO_0001 >= 1
               Begin
                 Select @VNIM_AU02 = ( @VNIM_AU01 + ISNULL(@CNIM_INGR,0)) - ISNULL(@CNIM_EGRE,0)
                 Select @VNIM_AU01 = @VNIM_AU02      
               End
      
            Fetch CU_TMSABA_MENS_U01 into @CSCO_BANC, @CSNU_CNTA_BANC,
            @CSAA_BNCO, @CSMM_BNCO, @CNIM_SALD_INIC, @CNIM_INGR, @CNIM_EGRE, @CSTI_SITU

            Select @VNCO_0001 = @VNCO_0001 + 1         


 
   End   /* ----- DEL WHILE ----- */

   Close CU_TMSABA_MENS_U01
   Deallocate CU_TMSABA_MENS_U01

Commit Tran

/*-------------------------- Fin -----------------------------*/


GO
